package com.laboratory.report.lab04.lab0402;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 运动项目
 */
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Data
public class Event {

    /**
     * 项目名称
     **/
    private String name;
    /**
     * 项目场地
     **/
    private String place;
    /**
     * 项目允许参与的人数
     **/
    private int persons;

    @Builder.Default
    private List<Athlete> athletes = new ArrayList<>();

    public Event(String name, String place, int persons) {
        this.name = name;
        this.place = place;
        this.persons = persons;
    }

    public void joinEvent(Athlete athlete) {
        if (athletes.size() >= persons) {
            System.out.println(name + "人数已满," + athlete.getName() + "无法参赛");
            return;
        }
        athletes.add(athlete);
        System.out.println("参与" + name + "项目的人有:" + athletes.stream().map(a->a.getName()).collect(Collectors.toList()));
    }

    public void display() {
        StringBuilder sb = new StringBuilder();
        sb.append("项目名称:").append(name)
                .append(", 项目场地:").append(place)
                .append(", 参与该项目运动员:").append(athletes.stream().map(a->a.getName()).collect(Collectors.toList()));
        System.out.println(sb);
    }

    public static Event fromLine(String line) {
        if(null == line || line.isEmpty()) {
            return null;
        }
        String[] lineArr = line.split(",");
        Event event = Event.builder()
                .name(lineArr[0])
                .place(lineArr[1])
                .persons(Integer.parseInt(lineArr[2]))
                .build();
        return event;
    }

    public String toLine() {
        return new StringBuilder().append(name).append("，")
                .append(place).append("，")
                .append(persons)
                .toString();
    }
}
